# 创建公共参数类
# 根据接口获取和新增公共参数
from Tools.requests_class import RequestClass
from Lib.common.common_function import *
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert
import ast
from Tools.environment import Environment


class CreateParams:
    host = Environment().interface_url()

    def get_token(self):
        # 获取请求的token
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/token.yaml')
        return RequestClass(self.host, 'post').get_value_interface(params=data[0], keyword='accessToken')

    def get_parent_grid_serial(self):
        '''
        获取网格父节点
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/get_parent_grid_serial.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        return RequestClass(self.host, 'post').get_value_interface(params=data[0], keyword='gridSerial')[1]

    def create_grid(self):
        '''
        新建网格
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_grid.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        data[0]['data']['parentSerial'] = self.get_parent_grid_serial()
        data[0]['data']['serial'] = self.get_parent_grid_serial()
        RequestClass(self.host, 'post').request(params=data[0])

    def create_business_event_type(self):
        '''
        创建商户相关的事件小类
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_business_event_type.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        # 设置大类id
        sql = 'SELECT id FROM info_event_category WHERE event_type_name LIKE "%市容%"'
        data[0]['data']['parentId'] = MysqlAssert().get_data(sql)
        RequestClass(self.host, 'post').get_value_interface(params=data[0], keyword='data')

    def create_site_event_type(self):
        '''
        创建工地相关的事件类型
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_site_event_type.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        # 设置大类id
        sql = 'SELECT id FROM info_event_category WHERE event_type_name LIKE "建设"'
        data[0]['data']['parentId'] = MysqlAssert().get_data(sql)
        RequestClass(self.host, 'post').get_value_interface(params=data[0], keyword='data')

    def create_safety_production_event_type(self):
        '''
        创建安全企业的事件类型
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_safety_production_event_type.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        # 设置大类id
        sql = 'SELECT id FROM info_event_category WHERE event_type_name LIKE "安全生产"'
        data[0]['data']['parentId'] = MysqlAssert().get_data(sql)
        RequestClass(self.host, 'post').get_value_interface(params=data[0], keyword='data')

    def create_site(self):
        '''
        创建建筑工地
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_site.yaml')
        site_data_path = get_path() + '/Tools/interface_params/site_data.txt'
        with open(site_data_path, mode='rt', encoding='utf-8') as f1:
            for line in f1:
                res = ast.literal_eval(line)
                for key, value in data[0]['data'].items():
                    if key in res:
                        data[0]['data'][key] = res[key]
        data[0]['headers']['accessToken'] = self.get_token()
        RequestClass(self.host, 'post').request(data[0])

    def create_business(self, image):
        '''
        新建商户
        :return:
        '''
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/create_business.yaml')
        data[0]['headers']['accessToken'] = self.get_token()
        data[0]['data']['images'] = image
        return RequestClass('post').get_value_interface(data[0], 'data')


if __name__ == '__main__':
    print(CreateParams().create_grid())
